layui.use(['form', 'layer', 'jquery', 'laydate'],
    function () {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer
            , laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#birthday',
            trigger: 'click'
        });

        //自定义验证规则
        // form.verify({
        //     nikename: function (value) {
        //         if (value.length < 5) {
        //             return '昵称至少得5个字符啊';
        //         }
        //     },
        //     pass: [/(.+){6,12}$/, '密码必须6到12位'],
        //     repass: function (value) {
        //         if ($('#L_pass').val() != $('#L_repass').val()) {
        //             return '两次密码不一致';
        //         }
        //     }
        // });
        let userId = sessionStorage.getItem("userId");
        let res = myAjax("/back/user/findById", {id: userId}, "get");
        console.log(res);
        setData(res.data);

        function setData(data) {
            // form.val('userAdd', {
            //     "username": data.username // "name": "value"
            //     , "email": data.email
            //     , "birthday": data.birthday //复选框选中状态
            //     , "phone": data.phone //开关状态
            //     , "sex": data.sex
            //     , "enable": data.enable + ""
            // });
            $("#username").val(data.username);
            $("#phone").val(data.phone);
            $("#sex").val(data.sex);
            $("#email").val(data.email);
            $('input[type=radio][name=enable][value=' + data.enable + ']').attr("checked", "checked");
            // $("#enable").val(data.enable + "");
            let hobby = data.hobby;
            let hobbys = hobby.split(",");
            $('input[type=checkbox][name=hobby]').each(function () {
                // arr_box.push($(this).attr("title"));
                for (let i = 0; i < hobbys.length; i++) {
                    if (hobbys[i] == $(this).attr("title")) {
                        $(this).attr("checked", "checked");
                    }
                }
            });
            //重新渲染表单
            form.render();
        }

        //监听提交
        form.on('submit(edit)',
            function (data) {
                data = data.field;
                let arr_box = [];
                $('input[type=checkbox][name=hobby]:checked').each(function () {
                    arr_box.push($(this).attr("title"));
                });
                //数组
                console.log(arr_box);
                data.hobby = arr_box.toLocaleString();
                data.id = userId;
                console.log(data);
                //发异步，把数据提交给php
                let res = myAjax("/back/user/edit", data, "post");
                if (res == null) {
                    layer.alert("编辑用户失败");
                    return;
                }
                if (res.count > 0) {
                    layer.alert("编辑用户成功", {
                            icon: 6
                        },
                        function () {
                            //关闭当前frame
                            xadmin.close();

                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                        });
                } else {
                    layer.alert("编辑用户失败");
                }

                return false;
            });

    });